﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class SysConfig
    {
        private readonly KeRui.DAL.SysConfig dal = new KeRui.DAL.SysConfig();

        private List<KeRui.Model.SysConfig> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.SysConfig> list = new List<KeRui.Model.SysConfig>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.SysConfig item = new KeRui.Model.SysConfig();
                    item.Item = dt.Rows[i]["Item"].ToString();
                    item.Key = dt.Rows[i]["Key"].ToString();
                    item.Name = dt.Rows[i]["Name"].ToString();
                    item.Value = dt.Rows[i]["Value"].ToString();
                    if (dt.Rows[i]["OrderID"].ToString() != "")
                    {
                        item.OrderID = int.Parse(dt.Rows[i]["OrderID"].ToString());
                    }
                    if (dt.Rows[i]["IsUsing"].ToString() != "")
                    {
                        item.IsUsing = int.Parse(dt.Rows[i]["IsUsing"].ToString());
                    }
                    item.Note = dt.Rows[i]["Note"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public DataSet GetDataSet(string Item, int IsUsing)
        {
            string strWhere = "Item='" + Utils.SqlStringFormat(Item) + "'";
            if ((IsUsing == 0) || (IsUsing == 1))
            {
                strWhere = strWhere + " and IsUsing=" + IsUsing;
            }
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetList("SysConfig", 0, 0, strWhere, "OrderID asc");
        }

        public DataSet GetDataSetByCache(string Item, int IsUsing)
        {
            string cacheKey = string.Concat(new object[] { "SysConfigDataSet-", Item, "-", IsUsing });
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetDataSet(Item, IsUsing);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (DataSet) cache;
        }

        public static int GetInt(string Item, string Key, int defValue)
        {
            return Utils.StrToInt(GetString(Item, Key), defValue);
        }

        public static int GetIntByCache(string Item, string Key, int defValue)
        {
            return Utils.StrToInt(GetStringByCache(Item, Key), defValue);
        }

        public List<KeRui.Model.SysConfig> GetList(string Item, int IsUsing)
        {
            string strWhere = "Item='" + Utils.SqlStringFormat(Item) + "'";
            if ((IsUsing == 0) || (IsUsing == 1))
            {
                strWhere = strWhere + " and IsUsing=" + IsUsing;
            }
            DataSet set = new KeRui.DAL.Common().GetList("SysConfig", 0, 0, strWhere, "OrderID asc");
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.SysConfig> GetListByCache(string Item, int IsUsing)
        {
            string cacheKey = string.Concat(new object[] { "SysConfigList-", Item, "-", IsUsing });
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(Item, IsUsing);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.SysConfig>) cache;
        }

        public KeRui.Model.SysConfig GetModel(string Item, string Key)
        {
            return this.dal.GetModel(Item, Key);
        }

        public KeRui.Model.SysConfig GetModelByCache(string Item, string Key)
        {
            string cacheKey = "SysConfigModel-" + Item + "-" + Key;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetModel(Item, Key);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.SysConfig) cache;
        }

        public static string GetName(string Item, string Key)
        {
            KeRui.Model.SysConfig model = new KeRui.BLL.SysConfig().GetModel(Item, Key);
            if (model == null)
            {
                return "";
            }
            return model.Name;
        }

        public static string GetNameByCache(string Item, string Key)
        {
            string cacheKey = "SysConfigName-" + Item + "-" + Key;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = GetName(Item, Key);
                    if (cache != null)
                    {
                        int num = 180;
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) num), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (string) cache;
        }

        public DataSet GetSelect(string Item, int IsUsing)
        {
            string strWhere = "Item='" + Utils.SqlStringFormat(Item) + "'";
            if ((IsUsing == 0) || (IsUsing == 1))
            {
                strWhere = strWhere + " and IsUsing=" + IsUsing;
            }
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("SysConfig", strWhere, "OrderID asc", "[Value],[Name]");
        }

        public DataSet GetSelectByCache(string Item, int IsUsing)
        {
            string cacheKey = string.Concat(new object[] { "SysConfigSelect-", Item, "-", IsUsing });
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetSelect(Item, IsUsing);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (DataSet) cache;
        }

        public static string GetString(string Item, string Key)
        {
            KeRui.Model.SysConfig model = new KeRui.BLL.SysConfig().GetModel(Item, Key);
            if (model == null)
            {
                return "";
            }
            return model.Value;
        }

        public static string GetStringByCache(string Item, string Key)
        {
            string cacheKey = "SysConfigValue-" + Item + "-" + Key;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = GetString(Item, Key);
                    if (cache != null)
                    {
                        int num = 180;
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) num), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (string) cache;
        }

        public void Update(KeRui.Model.SysConfig model)
        {
            this.dal.Update(model);
        }
    }
}

